package com.ikas.ai.server.module.system.controller;

import com.github.pagehelper.PageInfo;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.handle.JsonResultType;
import com.ikas.ai.server.aop.ColumnCheck;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.system.model.Dict;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.server.module.system.service.DictService;
import com.ikas.ai.utils.NumUtil;
import com.ikas.ai.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 字典管理类
 */
@SuppressWarnings("all")
@Validated
@RestController
@RequestMapping("/system/dict")
public class DictController extends AbstractBean {


    @Autowired
    private DictService dictService;
    private static final String COLUMN_NAMES = "id,type,code,name,value";

    /**
     * 获取字典分页数据
     *
     * @param request
     * @param type
     * @param code
     * @param name
     * @param pageNo
     * @param pageSize
     * @param sortColumnName
     * @param sortOrder
     * @return
     */
    @GetMapping("/list")
    public String pageQueryDict(HttpServletRequest request,
                                @RequestParam(value = "type", required = false) String type,
                                @RequestParam(value = "code", required = false) String code,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "page", required = false) Integer pageNo,
                                @RequestParam(value = "size", required = false) Integer pageSize,
                                @RequestParam(value = "sc", required = false) @ColumnCheck(value = COLUMN_NAMES) String sortColumnName,
                                @RequestParam(value = "so", required = false) String sortOrder) {
        JsonResult result = new JsonResult(JsonResultType.OK);
        PageUtil.configPageHelper(pageNo, pageSize, sortColumnName, sortOrder, "type", "desc");
        List<Dict> params = dictService.pageQueryDict(type, code, name);
        PageInfo<Dict> pageInfo = new PageInfo<>(params);
        result.add(pageInfo);
        return result.toJSON();
    }

    /**
     * 保存或修改字典信息
     *
     * @param request
     * @param param
     * @return
     */
    @PostMapping("/update")
    public String updateDict(HttpServletRequest request, @RequestBody Dict param) {
        JsonResult result = new JsonResult(JsonResultType.OK);
        Dict dictInfo = dictService.getDictInfo(param.getType(), param.getCode());
        Assert.isNull(dictInfo,"存在相同编码数据，请检查后提交");
        if (param.getId() != null) {
            dictService.updateDict(param);
            this.logOperation(request, LogOpsType.DICT_MANAGE, String.format("修改参数，参数类型为[%s]，参数编码[%s]", param.getType(), param.getCode()));
        } else {
            dictService.saveDict(param);
            this.logOperation(request, LogOpsType.DICT_MANAGE, String.format("保存参数，参数类型为[%s]，参数编码[%s]", param.getType(), param.getCode()));
        }
        return result.toJSON();
    }

    /**
     * 删除字典信息
     *
     * @param request
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    public String deleteDict(HttpServletRequest request, @PathVariable(name = "ids") String ids) {
        JsonResult result = new JsonResult(JsonResultType.OK);
        String[] ss = ids.split(",");
        List<Integer> arr = new ArrayList<>();
        for (String s : ss) {
            if (!NumUtil.isInteger(s)) continue;
            arr.add(Integer.parseInt(s));
        }
        dictService.deleteDict(arr);
        this.logOperation(request, LogOpsType.DICT_MANAGE, String.format("删除字典，字典ID为[%s]", ids));
        return result.toJSON();
    }

    /***
     * 通过字典type获取字典列表
     * @param type 参数 类型
     * @date 2021/12/1
     * @return {@link JsonResult}
     */
    @GetMapping("/dictList/{type}")
    public String dictList(@PathVariable("type") String type) {
        Assert.hasLength(type, "字典类型不能为空");
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        List<Dict> list = dictService.dictListByType(type);
        jsonResult.add(list);
        return jsonResult.toJSON();
    }

    /***
     * 通过字典type获取字典列表
     * @param type 参数 类型 多个类型用逗号拼接传递给后台
     * @date 2021/12/1
     * @return {@link JsonResult}
     */
    @GetMapping("/dictMap/{type}")
    public String dict(@PathVariable("type") String... type) {
        Assert.notEmpty(type, "字典类型不能为空");
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        Map<String, List<Dict>> map = dictService.dictListByTypes(type);
        jsonResult.add(map);
        return jsonResult.toJSON();
    }


}
